import {
  doLogin,
  doAgentLogin
} from '../../services/api/login';
var utilMd5 = require('~/utils/md5')
import Toast from 'tdesign-miniprogram/toast/index';
Page({
  data: {
    checkbox1: false,
    checkbox2: false,
    demoCheckboxMax: ['checkbox1', 'checkbox2'],
    passWord: '',
    userPhone: '',
  },
  async loginFn() {
    if (!this.data.checkbox1) {
      Toast({
        selector: '#t-toast',
        message: '请勾选用户协议及用户声明',
        icon: '',
        duration: 1000,
      });
      return
    }
    const patten = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!patten.test(this.data.userPhone)) {
      Toast({
        selector: '#t-toast',
        message: '请填写正确的手机号码',
        icon: '',
        duration: 1000,
      });
      return;
    };
    if (this.data.checkbox2) {
      wx.setStorageSync('reminberP', this.data.checkbox2);
      wx.setStorageSync('username', this.data.userPhone);
      wx.setStorageSync('password', this.data.passWord)
    }
    let {
      data
    } = await doAgentLogin({
      "passWord": utilMd5.hexMD5(this.data.passWord).toUpperCase(),
      "userPhone": this.data.userPhone
    });

    if (data && data.tokenId) {
      wx.setStorage({
        key: 'role',
        data: data,
      });
      wx.switchTab({
        url: '/pages/home/home',
      });
    }
  },

  navToAggrement() {
    wx.navigateTo({
      url: '/pages/aggrement/aggrement',
    })
  },

  navToPriamcy() {
    wx.navigateTo({
      url: "/pages/privary/privary",
    })
  },
  onCheckAllChange(e) {
    this.setData({
      checkbox1: e.detail.checked
    })
  },
  onCheckAllChange2(e) {
    this.setData({
      checkbox2: e.detail.checked
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let reminberP = wx.getStorageSync('reminberP');
    if (reminberP) {
      this.setData({
        userPhone: wx.getStorageSync('username'),
        passWord: wx.getStorageSync('password'),
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});